﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Entity;
using System.Runtime.InteropServices;

namespace PDAMain.Forms.InspectionWork
{
    public partial class InspectionToRepair :Form
    {
        public string assignmentId;
        private bool scanFlag = true;

        private string applyTime;
        private string reason;
        private string remarkStr;
        private string locatedid;

        private string showFlag = "";
        private bool tpFlag = true; //照相标记，控制是否能够保存

        public InspectionToRepair()
        {
            InitializeComponent();
            
        }

        private void InspectionToRepair_Load(object sender, EventArgs e)
        {
            bindData();
        }

        //加载数据
        private void bindData()
        {
            DataTable dt = DataHelper.InspectionWorkDao.getDetail(assignmentId).Tables[0];

            lblAssetNo.Text = dt.Rows[0]["ASSETNUM"].ToString();
            //lblAssetDescription.Text = dt.Rows[0]["ASSDESCRIPTION"].ToString();
            //lblLocation.Text = dt.Rows[0]["LOCATION"].ToString();
            //lblLocationDes.Text = dt.Rows[0]["LOCDESCRIPTION"].ToString();
            lblRepairPerson.Text = Identity.dutyPerson;
            locatedid = dt.Rows[0]["LOCATION"].ToString();

            cbQDescription.Items.Clear();
            cbQDescription.Items.Insert(0, "--请选择--");
            cbQDescription.Items.Insert(1, "资产损坏");
            cbQDescription.Items.Insert(2, "资产丢失");
            cbQDescription.SelectedIndex = 0;

        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //返回
            InspectionList list = new InspectionList();
            list.Show();
            this.Close();
            
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            //拍照
            if (tpFlag)
            {
                MessageBox.Show("请对要报修的资产拍照！");
                return;
            }
            if (cbQDescription.SelectedIndex == 0)
            {
                MessageBox.Show("请选择报修原因！");
                return;
            }
            if (MessageBox.Show("你确定要执行此操作吗？", "提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            string strRemark = cbQDescription.SelectedItem.ToString();
            Identity.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            applyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            remarkStr = txtremark.Text.Trim();
            reason = cbQDescription.SelectedItem.ToString();
            //报修状态为9
            if (DataHelper.InspectionWorkDao.Apply_Deal(assignmentId, "9", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strRemark))
            {
                if (DataHelper.ApplyToRepairDao.insertApplyRepair(lblAssetNo.Text.Trim(), lblRepairPerson.Text, applyTime, remarkStr, reason,locatedid))
                {
                    MessageBox.Show("成功同步数据至远程数据库");
                }
                else
                {
                    DataHelper.ApplyToRepairDao.insertLacolTable(lblAssetNo.Text.Trim(), lblRepairPerson.Text, applyTime, remarkStr, reason,"1",locatedid,"");
                    MessageBox.Show("同步数据失败，保存至本地数据库"); 
                }
            }
            else
            {
                bool Flag1=DataHelper.InspectionWorkDao.insertLacolTable(assignmentId, "9", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strRemark);
                bool Flag2 = DataHelper.ApplyToRepairDao.insertLacolTable(lblAssetNo.Text.Trim(), lblRepairPerson.Text, applyTime, remarkStr, reason, "1", locatedid,"");
                if (Flag1 && Flag2)
                {
                    MessageBox.Show("同步数据失败，保存至本地数据库"); 
                }
                else
                {
                    MessageBox.Show("操作失败");
                }
            }
            InspectionList list = new InspectionList();
            list.Show();
            this.Close();
            
        }

        private void InspectionToRepair_Closed(object sender, EventArgs e)
        {
        }

        private void backToList()
        {
            InspectionList list = new InspectionList();
            list.Show();
            this.Close();
        }

        private void btnPicture_Click(object sender, EventArgs e)
        {
            string folderName = "Inspection";
            string strAssetNo = lblAssetNo.Text.ToString();
            if (strAssetNo == string.Empty)
            {
                MessageBox.Show("请先扫描资产编号");
                return;
            }
            //维修前showFlag为空,维修中为1，维修后为2
            TakePicture tp = new TakePicture();
            string strFileName = "";
            try
            {
                if (showFlag == string.Empty)
                {
                    //strFileName = strAssetNo;
                    strFileName = assignmentId + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                    //tp.Save(strFileName);
                    pb1.Image = System.Drawing.Image.FromHbitmap(tp.Show(folderName, strFileName).GetHbitmap());
                    pb1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                    showFlag = "1";
                    tpFlag = false;
                }
                /* else if (showFlag == "1")
                 {
                     strFileName = strAssetNo;
                     //tp.Save(strFileName);
                     pb2.Image = System.Drawing.Image.FromHbitmap(tp.Show(strFileName).GetHbitmap());
                     pb2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                     showFlag = "2";
                 }
                 else if (showFlag == "2")
                 {
                     strFileName = strAssetNo + "_suf";
                     //tp.Save(strFileName);
                     pb3.Image = System.Drawing.Image.FromHbitmap(tp.Show(strFileName).GetHbitmap());
                     pb3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                     showFlag = "";
                     tpFlag = false;
                 }*/
            }
            catch
            {
                tp.dispose();
            }
            finally
            {
                tp.dispose();
            }
        }
    }
}